package com.lemon.model.baseModel;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.lemon.conf.content.IdTypeHandler;

public class BaseEntityImpl implements BaseEntity {

	private static final long serialVersionUID = 1L;
	private String id;
	private long idRaw = -1;
	

    private Date createTime; //插入逻辑在mysql实现，只查询
    private Date modifyTime; //更新逻辑在mysql实现，只查询

	/**
	 * 当前页码, 默认 1
	 */
	private Integer pageNum = 1;  // 当前页码
	/**
	 * 每页显示个数 , 默认 10
	 */
	private Integer pageSize = 10; // 每页显示多少个
	
	private String where = ""; //查询的where 条件 

	private String orderBy; //排序 要自己写 order by xx desc

	public String getId() {
		if (id == null && idRaw > 0)
//			id = IdTypeNullHandler.encode(idRaw);
			id = IdTypeHandler.encode(idRaw);
		return id;
	}

	public void setId(String id) {
		this.id = "0".equals(id) ? null : id;
	}

	long getIdRaw() {
		return idRaw;
	}

	void setIdRaw(long idRaw) {
		this.idRaw = idRaw;
	}

	public Date getCreateTime() {
		return this.createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Date getModifyTime() {
		return this.modifyTime;
	}

	public void setModifyTime(Date modifyTime) {
		this.modifyTime = modifyTime;
	}

	public Integer getPageNum() {
		return this.pageNum == null ? pageNum = 1 : pageNum;
	}

	public void setPageNum( Integer pageNum ) {
		this.pageNum = pageNum;
	}

	public Integer getPageSize() {
		return this.pageSize == null ? pageSize = 10 : pageSize;
	}

	public void setPageSize( Integer pageSize ) {
		this.pageSize = pageSize;
	}



	public String getWhere() {
		return this.where;
	}

	/**
	 * <b>Description:<blockquote> 自主拼装sql语句
	 * </blockquote></b>
	 * 
	 * @Title: setWhere
	 * @param where
	 * @param param
	 * @author Ran_cc
	 * @date 2018年11月2日
	 */
	public void setWhere(String where) {
		this.where = where;
	}

	public void setWhereAppend(String where) {
		//		this.where = this.where == null ? "" : this.where;
		this.where += where;
	}

	public void setWhereAppend(String where , Object... param) {
		//setWhere( where ,  param);
		//可以改造成 ? 占位符的形式
		if (where.indexOf("?") >= 0) {
			Pattern pattern = Pattern.compile("\\?");
			String text = where;
			int index = 0;
			StringBuffer buffer = new StringBuffer();
			Matcher matcher = pattern.matcher(text);
			while (matcher.find()) {
				matcher.appendReplacement(buffer , "'" + param[index].toString() + "'");
				index++;
			}
			matcher.appendTail(buffer);
			where = buffer.toString();
		}
		//		this.where = this.where == null ? "" : this.where;
		this.where += where;
	}

	/**
	 * <b>Description:<blockquote> 使用占位符形式复制
	 * </blockquote></b>
	 * 
	 * @Title: setWhere
	 * @param where
	 * @param param
	 * @author Ran_cc
	 * @date 2018年11月5日
	 */
	public void setWhere(String where , Object... param) {
		//可以改造成 ? 占位符的形式
		if (where.indexOf("?") >= 0) {
			Pattern pattern = Pattern.compile("\\?");
			String text = where;
			int index = 0;
			StringBuffer buffer = new StringBuffer();
			Matcher matcher = pattern.matcher(text);
			while (matcher.find()) {
				matcher.appendReplacement(buffer , "'" + param[index].toString() + "'");
				index++;
			}
			matcher.appendTail(buffer);
			where = buffer.toString();
		}
		this.where = where;
	}

	public String getOrderBy() {
		return this.orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	@Override
	public String toString() {
		return "" + (this.id != null ? "id=" + this.id + ", " : "") + "idRaw=" + this.idRaw + ", "
				+ (this.createTime != null ? "createTime=" + this.createTime + ", " : "")
				+ (this.modifyTime != null ? "modifyTime=" + this.modifyTime + ", " : "")
				+ (this.pageNum != null ? "pageNum=" + this.pageNum + ", " : "")
				+ (this.pageSize != null ? "pageSize=" + this.pageSize + ", " : "")
				+ (this.where != null ? "where=" + this.where + ", " : "")
				+ (this.orderBy != null ? "orderBy=" + this.orderBy : "") + "";
	}

	public static void main(String[] args) {
		Object[] ob = { "ss" , 0 , 100 };

		Pattern pattern = Pattern.compile("\\?");
		String text = "select a,b,c from table1 where a = ? and x = ? and cc= ? ";
		int index = 0;
		StringBuffer buffer = new StringBuffer();
		Matcher matcher = pattern.matcher(text);
		while (matcher.find()) {
			matcher.appendReplacement(buffer , "'" + ob[index].toString() + "'");
			index++;
		}
		matcher.appendTail(buffer);
		System.out.println(buffer.toString());
	}

}
